package be.kuleuven.thesis.neo4j;

import java.io.IOException;

import org.neo4j.graphdb.DynamicLabel;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Label;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Transaction;

import be.kuleuven.thesis.parsers.MyFeatureI;
import be.kuleuven.thesis.parsers.MyFeatureList;
import be.kuleuven.thesis.parsers.MyGeneIDGFF2Reader;

public class ImportingNonGenes {
	private static Neo4jDBManager dbManager;
	private static GraphDatabaseService myGraphDb;
	private static final Label nonGeneLabel = DynamicLabel.label("nongene");
	private static String propertyIndex = "locus_tag";
	
	
	
	public static void main(String[] args) throws IOException {
		dbManager = new Neo4jDBManager();
		myGraphDb = dbManager.getGraphDb();
		
		MyFeatureList features = MyGeneIDGFF2Reader.read("/home/moni/Documents/BioinformaticsMaster/Thesis/DataITM/SNP/ReferenceFiles/LD.ALL.M2.gff");
		
		dbManager.createIndexLabelProperty(nonGeneLabel, propertyIndex);
		
		try ( Transaction tx = dbManager.getGraphDb().beginTx()) {
			for (MyFeatureI feature : features) {
				if(! "CDS".equals(feature.type()))
					createNonGeneNode(feature);
			}
			tx.success();
		}

	}
	
	public static Node createNonGeneNode(MyFeatureI feature){
		String nonGeneId = feature.getAttribute(propertyIndex);
		
		Node nonGeneNode = myGraphDb.createNode(nonGeneLabel);
		nonGeneNode.setProperty(propertyIndex, nonGeneId);
		
		String [] location = feature.location().toString().trim().replaceAll("[\\[\\]]","").replace("=",":").split(";");
	    nonGeneNode.setProperty( "location", location);
	    nonGeneNode.setProperty("seqType", feature.type());
	    nonGeneNode.setProperty("seqId", feature.seqname().toString());
        nonGeneNode.setProperty("strand", String.valueOf(feature.location().bioStrand()));
        
		if("ncRNA".equals(feature.type()))
	        nonGeneNode.setProperty("ncRNA_class",feature.getAttribute("ncRNA_class"));    
		
		
		if(feature.hasAttribute("product"))
			nonGeneNode.setProperty("product", feature.getAttribute("product"));
		
		return nonGeneNode;
		
	}
	
}
